<?php

namespace IDock\Insurance\TPIC\Request;

use Exception;
use IDock\Insurance\TPIC\Request\AllTPICRequest;
use IDock\Insurance\TPIC\Response\SaveVehicleResponse;
use IDock\Insurance\TPIC\Response\VehicleModelResponse;
use IDock\Insurance\TPIC\TPICEnums;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\Premium\PremiumUnderwritingVisitor;
use IDock\Visitor\VehicleModelVisitor;

/**
 * 保存车辆信息
 */
class SaveVehicleRequest extends AllTPICRequest
{
    protected $response = SaveVehicleResponse::class;

    protected $urlTip = '保存车辆信息';

    public $route = 'saveOrderVehicle';

    /**
     * @var PremiumInquiryVisitor $visitor
     */
    protected $visitor;

    public function generate(): array
    {
        $vehicle = $this->visitor->getVehicleVisitor();
        $arr = [
            'order' => [
                'orderNo' => $this->visitor->getExtInfoVisitor()->argc['orderNo'] ?? '',
            ],
            'sales' => [
                'cityCode' => '510100',//成都市
                'solutionCode' => '',//子协议代码
                'groupNo' => $this->visitor->getExtInfoVisitor()->argc['groupNo'] ?? '',//团队号 510100
                'salesmanCode' => '',//业务员代码
            ],
            'carOwner' => [
                'name' => $this->visitor->getOwnerVisitor()->name,
                'identifyNumber' => $this->visitor->getOwnerVisitor()->certificateNo,
                'ownerNature' => $this->visitor->getOwnerVisitor()->typeCode == 'R1' ? '01' : '03',//车主性质
                'identifyType' => TPICEnums::$certificateTypeCode[$this->visitor->getOwnerVisitor()->certificateTypeCode] ?? '',//车主证件类型 默认 01 身份证
            ],
            'vehicle' => [
                'licenseNo' => $vehicle->licenseNo ?: '暂未上牌' ,
                'frameNo' => $vehicle->vin,
                'engineNo' => $vehicle->engineNo,
                'modelCode' => $vehicle->vehicleModelCode,
                'chgOwnerFlag' => $vehicle->isTransfer ? '01' : '0',//过户车标识
                'singeinDate' => $vehicle->firstRegisterDate,//登记时间
                'useNatureShow' => TPICEnums::$useNatureCode[$vehicle->useNatureCode] ?? '',//车辆使用性质
                'carUseType' => TPICEnums::$causeType[$vehicle->causeType],//车辆用途
//                'driveUseAttribute' => '',//行驶证使用性质
                'vehicleCategory' => $vehicle->vehicleStyleCode,//交管车辆类型
                'tonCount' => $vehicle->vehicleTonnage,//核定载质量
                'completeKerbMass' => $vehicle->wholeWeight,//整备质量
//                'carKindCodeShow' => '',//货车和特种车必录
                'carKindCode' => $vehicle->carKindCode,//机动车种类
                'seatCount' => $vehicle->vehicleSeat,//机动车种类

            ],
        ];

        //商业险过户
        if (boolval($this->visitor->getVehicleVisitor()->isTransfer)) {
            $arr['vehicle']['transferDate'] = $vehicle->isTransfer ? $vehicle->transferDate : '';//过户时间
            $arr['vehicle']['bschgOwnerFlag'] = '01';
            $arr['vehicle']['chgOwnerFlag'] = '01';
        }

        //交强险过户
        if (boolval($this->visitor->getVehicleVisitor()->isCiTransfer)) {
            $arr['vehicle']['transferDate'] = $vehicle->isTransfer ? $vehicle->transferDate : '';//过户时间
            $arr['vehicle']['bzchgOwnerFlag'] = '01';
            $arr['vehicle']['chgOwnerFlag'] = '01';
        }

        if(empty($arr['carOwner']['identifyType'])){
            throw new Exception("不支持该车主证件类型");
        }
        return $arr;
    }

}
